package snowforce;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;

import snowforce.datatype.RouteEdgeJSON;
import snowforce.datatype.EvaluationJSON;
import snowforce.datatype.HintJSON;
import snowforce.datatype.MapJSON;
import snowforce.datatype.PassPointJSON;
import snowforce.datatype.RouteJSON;

@Provider
public final class JAXBContextResolver implements ContextResolver<JAXBContext>
{

	private final JAXBContext	context;

	private final Set<Class>	types;

	private final Class[]		cTypes	= { HintJSON.class, PassPointJSON.class, EvaluationJSON.class, MapJSON.class, RouteJSON.class, RouteEdgeJSON.class, ArrayList.class };

	public JAXBContextResolver() throws JAXBException
	{
		this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
		this.types = new HashSet(Arrays.asList(cTypes));
	}

	public JAXBContext getContext(Class<?> objectType)
	{
		return (types.contains(objectType)) ? context : null;
	}
}